<?php

class Core_Frontend_Module_Subject_Concessions extends Dcmp_Module_Base {

    protected $name = 'subject_concessions';

    public function install() {

    }

    public function uninstall() {

    }

    public function buildConfig($form) {
        $form->addTextBoxField($this->__('subject.label.subject_id'), 'subject_id', 'module[parameters][dynamic][subject_id]');
        $form->createTab('module_option', $this->__('design.label.dynamic_parameter'),
                array('field_subject_id'));
    }

    public function render() {
        $subject = $this->getVariables()->getData('subject_object');
        if(!$subject){
            $subjectId = $this->getDynamicParameter('id');
            if(!$subjectId){ $subjectId = 1;}
            Kd_Web::getVariables()->setData('subject_id',$subjectId);
            $subject = App::getModel('subject/content')->load($subjectId)->updateClickCount();
        }
        $poiIds = array();
        $concessionsPois = array();
        $concessions = $subject->getConcessions()->load();
        $roads = $subject->getRoads()->load();
        foreach ($concessions as $concession) {
            $arr = $concession->getPoiIds();
            if(!empty($arr)){
                $poiIds[] = $arr[0];
            }
        }
        if (count($poiIds) > 0) {
            $concessionsPois = App::getModel('foundation/collection_poi')
                            ->distinct()
                            ->filterByIds($poiIds)
                            ->load();
        }
        $tmpl = $this->getTemplate()->assignMulti(array('subject' => $subject,'concessions_pois'=>$concessionsPois,'concessions'=>$concessions,'roads'=>$roads));
        return $tmpl->render($this->getTemplatePath($this->getValueByKey('template_name'),$this->getValueByKey('template_script')));
    }

}